package com.song.shop.nio.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

/**
 * @author Song Yinghui
 * @date 2019/2/14
 * @Version 1.0
 */
public class NIOClient {

    public static void start(){
        try {
            SocketChannel socketChannel = SocketChannel.open();
            Selector selector = Selector.open();
            socketChannel.configureBlocking(false);
            if(socketChannel.connect(new InetSocketAddress("127.0.0.1" , 8888))){
                socketChannel.register(selector, SelectionKey.OP_READ);
                doWrite(socketChannel);
            }else{
                socketChannel.register(selector,SelectionKey.OP_CONNECT);
            }
            System.out.println("wait  recivice----");
            selector.select(10000);
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()){
                SelectionKey key = iterator.next();
                iterator.remove();
                SocketChannel channel = (SocketChannel) key.channel();
                if (key.isValid()){
                    if (key.isConnectable()){
                        if (channel.finishConnect()){
                            channel.register(selector,SelectionKey.OP_READ);
                            doWrite(channel);
                        }
                    }
                    if (key.isReadable()){
                        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                        int len = 0 ;
                        String str = "";
                        while ((len = channel.read(byteBuffer)) > 0){
                            str = new String(byteBuffer.array(), 0, len);
                            System.out.println(str);
                            byteBuffer.clear();
                        }

                        /*int bytes = channel.read(bf);
                        if (bytes > 0) {
                            bf.flip();
                            byte[] byteArray = new byte[bf.remaining()];
                            bf.get(byteArray);
                            String responseMessage = new String(byteArray, "UTF-8");
                            System.out.println("=======The response message is："+responseMessage);
                        }*/
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void doWrite(SocketChannel socketChannel) {

        byte[] requestBytes = "我是客户端 client".getBytes();
        ByteBuffer bf = ByteBuffer.allocate(requestBytes.length);
        bf.put(requestBytes);
        bf.flip();
        try {
            socketChannel.write(bf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Thread(()->start()).start();
    }
}
